package org.zuel.boot.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.zuel.boot.demo.entity.*;
import org.zuel.boot.demo.exception.ParameterException;
import org.zuel.boot.demo.mapper.CompanyMapper;
import org.zuel.boot.demo.mapper.EmploymentMapper;
import org.zuel.boot.demo.mapper.StudentMapper;
import org.zuel.boot.demo.mapper.SysUserMapper;
import org.zuel.boot.demo.service.IEmploymentService;
import org.zuel.boot.demo.web.JsonResult;
import org.zuel.boot.demo.web.PageParam;
import org.zuel.boot.demo.web.PageResult;

import java.util.Arrays;
import java.util.List;


/**
 *  服务实现类
 *
 * @author ikun
 */
@Service
public class EmploymentServiceImpl extends ServiceImpl<EmploymentMapper, Employment> implements IEmploymentService {

    private CompanyMapper companyMapper;
    private StudentMapper studentMapper;
    private SysUserMapper sysUserMapper;

    @Autowired
    public EmploymentServiceImpl(CompanyMapper companyMapper, StudentMapper studentMapper, SysUserMapper sysUserMapper) {
        this.companyMapper = companyMapper;
        this.studentMapper = studentMapper;
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public IPage<Employment> list1(PageParam<Employment> pageParam, SysUser user) {
        // 获取SysUser的id
        Integer employeeId = user.getUserId();

        // 设置查询条件
        QueryWrapper<Employment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Employee_id", employeeId);
        queryWrapper.eq("state",0);

        // 分页查询
        Page<Employment> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }
    @Override
    public IPage<Employment> list2(PageParam<Employment> pageParam, SysUser user) {
        // 获取SysUser的id
        Integer employeeId = user.getUserId();

        // 设置查询条件
        QueryWrapper<Employment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Employee_id", employeeId);
        queryWrapper.eq("state",1);

        // 分页查询
        Page<Employment> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }
    @Override
    public IPage<Employment> list3(PageParam<Employment> pageParam, SysUser user) {
        // 获取SysUser的id
        Integer employeeId = user.getUserId();

        // 设置查询条件
        QueryWrapper<Employment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Employee_id", employeeId);

        // 分页查询
        Page<Employment> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }
    @Override
    public IPage<Employment> list11(PageParam<Employment> pageParam, SysUser user) {
        // 获取SysUser的id
        Integer employerId = user.getUserId();

        // 设置查询条件
        QueryWrapper<Employment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Employer_id", employerId);
        queryWrapper.eq("state",1);

        // 分页查询
        Page<Employment> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }
    @Override
    public IPage<Employment> list21(PageParam<Employment> pageParam, SysUser user) {
        // 获取SysUser的id
        Integer employerId = user.getUserId();

        // 设置查询条件
        QueryWrapper<Employment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Employer_id", employerId);
        queryWrapper.eq("state",0);

        // 分页查询
        Page<Employment> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }
    @Override
    public IPage<Employment> list31(PageParam<Employment> pageParam, SysUser user) {
        // 获取SysUser的id
        Integer employerId = user.getUserId();

        // 设置查询条件
        QueryWrapper<Employment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Employer_id", employerId);

        // 分页查询
        Page<Employment> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public JsonResult read(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        Employment employment = baseMapper.selectById(id);
        return JsonResult.ok().setData(employment);
    }

    @Override
    public JsonResult add(Employment employment, SysUser user) {
        if (employment ==null){
            throw new ParameterException("参数对象为空");
        }
        QueryWrapper<Company> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("name", employment.getEmployerName());
        Company company = companyMapper.selectOne(queryWrapper1);
        employment.setEmployeeId(user.getUserId());
        employment.setEmployerId(company.getUserId());
        employment.setEmployerName(company.getName());
        employment.setState(0);
        QueryWrapper<SysUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", user.getUserId());
        SysUser sysUser=sysUserMapper.selectOne(queryWrapper2);
        employment.setEmployeeName(sysUser.getName());
        baseMapper.insert(employment);
        return JsonResult.ok("申请成功");
    }
    @Override
    public JsonResult add1(Employment employment, SysUser user) {
        if (employment == null) {
            throw new ParameterException("参数对象为空");
        }
        QueryWrapper<Student> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("xm", employment.getEmployeeName());
        List<Student> students = studentMapper.selectList(queryWrapper1);
        if (students.isEmpty()) {
            throw new ParameterException("未找到对应的学生");
        }
        // 选择第一个匹配的学生
        Student student = students.get(0);
        //此时公司为用户，但仍是雇佣者
        employment.setEmployerId(user.getUserId());
        employment.setEmployeeId(student.getUserId());
        employment.setEmployeeName(student.getXm());
        employment.setState(1);

        QueryWrapper<SysUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", user.getUserId());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper2);
        if (sysUser == null) {
            throw new ParameterException("未找到对应的用户");
        }
        employment.setEmployerName(sysUser.getName());
        baseMapper.insert(employment);
        return JsonResult.ok("申请成功");
    }

    @Override
    public JsonResult update(Employment employment) {
        if (employment == null) {
            throw new ParameterException("参数对象为空");
        }

        if (employment.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }

        baseMapper.updateById(employment);
        return JsonResult.ok("修改成功");
    }
    @Override
    public JsonResult agree(Employment employment) {
        if (employment == null) {
            throw new ParameterException("参数对象为空");
        }

        if (employment.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }
        employment.setState(2);
        baseMapper.updateById(employment);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult delete(Integer[] ids) {
        if (ids == null) {
            throw new ParameterException("参数对象为空");
        }

        if (ids != null || ids.length > 0) {
            baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return JsonResult.ok("删除成功");
    }

}